var portaluserno = getCookie ('portaluserno');
var configInfo=InitialParameter("/Config.json");
// let modelApi = configInfo["modelTree"].type + '://' + configInfo["modelTree"].value
var modelApi = getBaseplatformPort () ;
let uploadApi = configInfo["fileUploadOrigin"].type + '://' + configInfo["fileUploadOrigin"].value
// let uploadApi1 = 'http://192.168.100.123:8886'

let shaType = true
let applicationName = false
let paramsObj = {
    registerUser:portaluserno,
    systemType:'',
    analysisTools:'',
    systemStatus:1,
    name:'',
    analysisToolValus:'',
    databaseName:'',
    tableName:'',
    type:'',
    shpPath:'',
    description:''
    // typeData:'',  paramsObj.databaseName
    // typeDataTable:'',
    // analysisTools:'',
    // applicationClassification:'',
    // applicationName:''
}
$(document).ready(function () {
    if (!portaluserno) {
        location.href="/"
    } else {
        getData ()
        whetherUploadUrl ()
    }
})
function whetherUploadUrl() {
    if (shaType) {
        paramsObj.type = 'table'
        $('#selectDatabase').attr('lay-verify','required')
        $('#selectDatabaseTable').attr('lay-verify','required')
    } else {
        paramsObj.type = 'shp'
    }
}
/*

    private String registerUser;//注册用户  admin
    private String registerDate;//注册时间 不用给了
    private String systemType;//应用分类     ZDVALUE
    private String analysisTools;//分析工具    ZDNAME
    private String datasourcePath;//数据源路径  空
    private String systemStatus;//应用状态 1
    private String approvalStatus;//审批状态 空着
    private String name;//名称 应用名称
    private String config;//配置信息  空的
applicationSystem/dispose
    @TableField(exist = false)
    private String analysisToolValus;//分析工具url  ZDVALUE
    @TableField(exist = false)
    private String databaseName;//数据库名称   选择数据库 测试库  左边
    @TableField(exist = false)
    private String tablename;//表名  右边   TABLE_NAME
    @TableField(exist = false)
    private String type;//类型：是不是shp  上传的话type=shp   不上传type=table

*/
layui.use(['form','upload','element','layer'], function(){
    var form = layui.form
    var upload = layui.upload
    var element = layui.element 
    var layer = layui.layer
    var uploadInst = upload.render({
        elem: '#uploadFiles',
        auto: false,
        url: uploadApi + "/sw/upload/uploadZip",
        accept: 'file'
        ,exts: 'zip',
        choose: function(res){
            res.preview(function(index,file,result){
                var myFormData = new FormData();
                myFormData.append('userNo',portaluserno);
                myFormData.append('file',file);
                $.ajax({
                    type: "post",
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    url: uploadApi + "/sw/upload/uploadZip",
                    data: myFormData,
                    contentType: false,
                    processData: false,
                    success: function (res) {
                        res = JSON.parse(res);
                        if (res.code == 200) {
                            paramsObj.shpPath = res.data
                            $('#selectDatabase').removeAttr('lay-verify')
                            $('#selectDatabaseTable').removeAttr('lay-verify')
                            $('#selectDatabase').attr('disabled',true)
                            $('#selectDatabaseTable').attr('disabled',true)
                            shaType = false
                            whetherUploadUrl();
                        }
                        layer.msg(res.msg)
                    },
                    error: function (err) {
                        debugger
                    }
                });
            });
        },
        done: function(res){},
        error: function(){ console.log('失败'); }
    });
    form.on('submit(component-form-demo1)', function(data){
        blurInput ()
        if (applicationName) {
            paramsObj.name = $('.applicationName').val()
            paramsObj.description = $('.userShuoMing').val()
            $.ajax({
                type: "post",
                async: false,
                url: modelApi + "apply/add",
                data:paramsObj,
                datatype: "json",
                success: function (res) {
                    let data = JSON.parse(res)
                    if (data.code == 200) {
                        parent.layer.closeAll();
                        layui.layer.msg('保存成功')
                    } else {
                        layui.layer.msg(data.msg, {time:1000, icon:5, shift:6})
                    }
                    layui.form.render("select");
                }
            });
        } else {
            layui.layer.msg('应用名称重复,请重新输入',{time:1000, icon:5, shift:6})
        }
        // layer.msg(JSON.stringify(data.field));
        // return false;
    });
    form.on('select(typeData)', function(data){
        if (data.value == '') {
            // 打开按钮
            $('#selectDatabaseTable').empty()
            $('#uploadFiles').attr('disabled',false)
            $('#uploadFiles').removeClass('layui-btn-disabled')
        } else {
            // 禁用按钮
            $('#uploadFiles').attr('disabled',true)
            $('#uploadFiles').addClass('layui-btn-disabled')
        }
        /*
        if (data.value != '') {
            paramsObj.tableName = data.value
            $('#uploadFiles').attr('disabled',true)
            $('#uploadFiles').addClass('layui-btn-disabled')
        } else {
            $('#uploadFiles').attr('disabled',false)
        }
        */
        paramsObj.databaseName = data.value
        let userName = portaluserno
        let tableName = data.value
        if (tableName) {
            $.ajax({
                type: "post",
                async: false,
                url: modelApi + "dababaseConnect/getTableByName",
                data:{
                    userName,tableName
                },
                datatype: "json",
                success: function (res) {
                    let data = JSON.parse(res)
                    if (data.code == 200) {
                        data.data.map(item => {
                            $('#selectDatabaseTable').append(`<option value="${item.TABLE_NAME}" label="${item.TABLE_ALIAS}">${item.TABLE_NAME}</option>`)
                        })
                    } else {
                        layui.layer.msg(data.msg, {time:1000, icon:5, shift:6})
                    }
                }
            });
        }
        layui.form.render("select");
    });
    form.on('select(typeDataTable)', function(data){
        if (data.value != '') {
            paramsObj.tableName = data.value
            $('#uploadFiles').attr('disabled',true)
            $('#uploadFiles').addClass('layui-btn-disabled')
        } else {
            $('#uploadFiles').attr('disabled',false)
        }
    })
    // 分析工具
    form.on('select(analysisTools)', function(data){
        let arr = data.value.split(',')
        paramsObj.analysisTools = arr[0]
        paramsObj.analysisToolValus = arr[1]
    })
    // 应用分类
    form.on('select(applicationClassification)', function(data){
        paramsObj.systemType = data.value
    })
});
let zhutiList = []
// 表单失去焦点
function blurInput () {
    let name = $('.applicationName').val()
    if (name != '' ){
        $.ajax({
            type: "post",
            async: false,
            url: modelApi + "apply/getApplyByName",
            data:{
                name
            },
            datatype: "json",
            success: function (res) {
                let data = JSON.parse(res)
                if (data.code == 500) {
                    applicationName = true
                } else {
                    applicationName = false
                    layui.layer.msg('应用名称重复,请重新输入',{time:1000, icon:5, shift:6})
                }
            }
        })
    }
}
function getData () {
    // 选择数据库第一表格数据的获取
    $.ajax({
        type: "post",
        async: false,
        url: modelApi + "dababaseConnect/getDataBaseList",
        data:{},
        datatype: "json",
        success: function (res) {
            let data = JSON.parse(res)
            if (data.code == 200) {
                data.data.map(item => {
                    $('#selectDatabase').append(`<option value="${item.NAME}" label="${item.NAME}">${item.NAME}</option>`)
                })
                layui.form.render("select");
            } else {
                layui.layer.msg(data.msg, {time:1000, icon:5, shift:6})
            }

        }
    });
    // 分析工具 数据的获取
    $.ajax({
        type: "post",
        async: false,
        url: modelApi + "dict/getDataDict",
        data:{
            name:'analysisType'
        },
        datatype: "json",
        success: function (res) {
            let data = JSON.parse(res)
            if (data.code == 200) {
                data.data.map(item => {
                    $('#analysisTools').append(`<option value="${item.ZDTITLE},${item.ZDVALUE}" label="${item.ZDTITLE}">${item.ZDTITLE}</option>`)
                })
                layui.form.render("select");
            } else {
                layui.layer.msg(data.msg, {time:1000, icon:5, shift:6})
            }

        }
    });
    // 应用分类 数据的获取
    $.ajax({
        type: "post",
        async: false,
        url: modelApi + "dict/getDataDict",
        data:{
            name:'spatial'
        },
        datatype: "json",
        success: function (res) {
            let data = JSON.parse(res)
            if (data.code == 200) {
                data.data.map(item => {
                    $('#applicationClassification').append(`<option value="${item.ZDVALUE}" label="${item.ZDTITLE}">${item.ZDTITLE}</option>`)
                })
                layui.form.render("select");
            } else {
                layui.layer.msg(data.msg, {time:1000, icon:5, shift:6})
            }

        }
    });
}